package cn.ymz.view;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ymz.App;
import cn.ymz.component.FxDialogs;
import cn.ymz.model.Urls;
import cn.ymz.model.User;
import cn.ymz.service.LoginService;
import cn.ymz.service.impl.LoginServiceImpl;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.net.URI;
import java.util.List;

/**
 * Dialog to edit details of a person.
 *
 * @author Marco Jakob
 */
public class LoginViewController {

    private static final Log log = LogFactory.get();

    private App mainApp;

    @FXML
    private TextField username;
    @FXML
    private TextField password;
    @FXML
    private ComboBox<String> comboBox;
    @FXML
    private ImageView imageView;

    private LoginService loginService = LoginServiceImpl.getInstance();

    private Urls urls = Urls.getInstance();


    @FXML
    private void initialize() {
        Image image = new Image("img/xiaosanlang.png");
        imageView.setImage(image);

        List<String> urlsList = loginService.getDomainUrl();
        comboBox.getItems().addAll(urlsList);
        // 获取选中记录
        try {
            FileReader reader = new FileReader("Login.lmt");
            String info = reader.readString();
            if (!StrUtil.isEmpty(info)) {
                String[] split = info.split(",");
                username.setText(split[0]);
                password.setText(split[1]);
                comboBox.getSelectionModel().select(split[2]);
                urls.setBaseUrl(split[2]);
            }
        } catch (Exception ignored) {
        }

        // 监听选项改变
        comboBox.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> setBaseUrl(newValue));
    }

    private void setBaseUrl(String url) {
        urls.setBaseUrl(url);
    }

    @FXML
    private void handleLogin() {
        String usernameText = username.getText();
        String passwordText = password.getText();
        log.info("用户名：" + usernameText + ",密码:" + passwordText);
        User user = new User();
        user.setMobile(usernameText);
        user.setPassword(passwordText);
        String login = loginService.login(user);

        // 获取选择的domain
        String selectedItem = comboBox.getSelectionModel().getSelectedItem();

        if (!StrUtil.isEmpty(login) && !login.startsWith("false")) {
            boolean del = FileUtil.del("Login.lmt");
            String info = user.getMobile() + "," + user.getPassword() + "," + selectedItem;
            FileWriter fileWriter = new FileWriter("Login.lmt");
            fileWriter.write(info);
            // 展示 拨号视图
            mainApp.showCallView();
        } else {
            String[] split = login.split("false:");
            FxDialogs.showError("登录失败", split[1]);
        }

    }

    public void setMainApp(App mainApp) {
        this.mainApp = mainApp;
    }


}
